<template>
	<view class="content">
		<search-input></search-input>
		<view class="current-address">
			<view class="location-content">
				<view class="address-title">{{ currentEqiupment.eqiupment_name }}</view>
				<view class="locate-content">
					<view class="locate-images">
						<image style="width: 100%; height: 100%;" mode="aspectFit" :src="locate"></image>
					</view>
					<view class="locate-title">重新定位</view>
				</view>
			</view>
			<view class="address-items">
				{{ currentEqiupment.address }}
			</view>
		</view>
		<view class="search-list">
			<view class="list-items" v-for="(items, index) in list" :key="index" @click="chooseAddress(items)">
				<view class="address-title">
					{{ items.name }}
				</view>
				<view class="address-items">
					<view class="address-content">
						{{ items.address }}
					</view>
					<view class="address-distance">
						{{ items.distance }}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex';
	import searchInput from '@/components/search-input/index.vue';
	export default {
		components: {
			searchInput
		},
		data() {
			return {
				shearchName: "",
				list: [],
				locate: '/static/images/icons/icon_dw@3x.png'
			}
		},
		computed: {
			...mapGetters([
				'goodsList', 'currentEqiupment'
			])
		},
		created() {
			let token = "";
			uni.getStorage({
				key: 'uerInfo',
				success: function(res) {
					token = res.data.token
				}
			});
			if (!token) {
				let key = this.getUrlKey('key')
				console.log('key', key)
				this.getMess(key);
			}
			this.getList();
		},
		methods: {
			getMess(key) {
				const path = {
					app: "wx",
					api: "api_wx_user_login",
					version: "1.0",
					type: "1",
					data: {
						key: key
					}
				}
				const _this = this;
				_this.sendSock(path, function(res) {
					_this.getSock(res.data)
				})
			},
			getList() {
				const path = {
					app: "wx",
					api: "api_wx_index_getEquipmentList",
					version: "1.0",
					type: "1,2",
					data: {
						region_id: 350203,
						longitude: "24.48732",
						latitude: "118.198251"
					}
				}
				const _this = this;
				_this.sendSock(path, function(res) {
					_this.getSock(res.data)
				})
			},
			clearSearch() {
				this.shearchName = ""
			},
			chooseAddress(data) {
				var pages = getCurrentPages();
				var currPage = pages[pages.length - 1]; //当前页面
				var prevPage = pages[pages.length - 2]; //上一个页面
				//将前一页的address和address_id修改为当前选中的
				// prevPage._data.name = data.name;
				this.$set(this.currentEqiupment, 'eqiupment_id', data.id)
				this.$set(this.currentEqiupment, 'eqiupment_name', data.name)
				this.$set(this.currentEqiupment, 'address', data.address)
				uni.navigateBack()
			},
			getSock(res) {
				if (res !== '{"type":"ping"}') {
					const resj = JSON.parse(res);
					if (resj.code === -1) {
						uni.showToast({
							icon: "none",
							title: resj.message,
							duration: 2000
						});
					} else {
						if (resj.api === "api_wx_user_login") {
							console.log('key登录', resj.data)
							this.$store.commit('login', resj.data);
							this.temporary()
						} else if (resj.api === "api_wx_user_keepLogin") {
							console.log('token登录', resj.data)
							this.temporary()
						} else if (resj.api === "api_wx_index_getEquipmentList") {
							console.log('this.list', resj.data)
							this.list = resj.data
						}
					}
				}
			}
		}
	}
</script>

<style>
	.content {
		padding: 30rpx;
	}

	.address-title {
		font-size: 36rpx;
		font-family: 'PingFang SC';
		font-weight: bold;
		padding: 20rpx 0;
	}

	.address-items {
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-family: 'PingFang SC';
		font-size: 24rpx;
		font-weight: 500;
		color: #999999;
	}

	.current-address {
		margin-top: 30rpx;
		box-shadow: 0rpx 0rpx 20rpx 0rpx rgba(0, 0, 0, 0.1);
		border-radius: 10rpx;
		padding: 0 20rpx 20rpx 20rpx;
	}

	.search-list {
		padding: 0 20rpx;
	}

	.location-content {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.locate-images {
		width: 24rpx;
		height: 30rpx;
		display: flex;
		align-items: center;
	}

	.locate-content {
		display: flex;
		align-items: center;
	}

	.locate-title {
		margin-left: 12rpx;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 500;
	}
</style>
